package com.buba.system.controller;

import com.buba.system.pojo.Employee;
import com.buba.system.pojo.Menu;
import com.buba.system.pojo.Page;
import com.buba.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Menu")
public class MenuController {
    private MenuService menuService;

    @RequestMapping("/getMenu")
    public Map<String, Employee> getMenu(HttpSession session) {
        Employee employee = (Employee) session.getAttribute("employee");
        Map<String, Employee> map = new HashMap<>();
        map.put("employee", employee);
        return map;
    }
    @RequestMapping("/selMenuAll")
    public Map<String,Object> selMenuAll(){
        Map<String,Object> map = new HashMap<>();
        map.put("list",menuService.selMenuAll());
        return map;
    }

    /**
     * 查询菜单方法
     * @param map
     * @return
     */
    @RequestMapping("/selMenuTable")
    public Page selMenuTable(@RequestParam Map<String, Object> map){
        Page<Menu> page = new Page<>(Integer.parseInt(map.get("pageNow").toString()));
        map.put("page", page);
        List<Menu> list = null;
        //查询菜单总条数
        page.computePageTotal(menuService.selMenuCount());
        //查询所有菜单
        list = menuService.selMenuTable(page);
        //把list装入page类
        page.setList(list);
        return page;
    }

    /**
     * 根据职位查询菜单列表 回显
     * @param jobId
     * @return
     */
    @RequestMapping("/selMenuByJobId")
    public Map<String, Object> selMenuByJobId(Integer jobId){
        Map<String, Object> map = new HashMap<>();
        //1、查询所有的菜单列表
        List<Menu> list = menuService.selMenuAll();
        // 2、查询该职位拥有的菜单列表
        List<Menu> jobMenuList = menuService.selMenu(jobId);
        loop:
        for (Menu menu : list) {
            for (Menu m : jobMenuList) {
                if (menu.getMenuId() == m.getMenuId()) {
                    menu.setChecked(true);
                    continue loop;
                }
            }
        }

        map.put("list",list);
        return map;
    }


    @Autowired
    @Qualifier("menuServiceImpl")
    public void setMenuService(MenuService menuService) {
        this.menuService = menuService;
    }
}
